package com.suredy.app.notice.service;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import com.suredy.app.notice.model.Notice;
import com.suredy.core.service.BaseSrvWithFlowEntity;

/**
 * 公告服务类
 * 
 * @author VIVID.G
 * @since 2015-4-13
 * @version v0.1
 */
@Service
public class NoticeSrv extends BaseSrvWithFlowEntity<Notice> {

	public NoticeSrv() {
		super.addAnDefOrder("publishTime", false);
		super.addAnDefOrder("publicityPeriod", true);
	}

	@Override
	public DetachedCriteria getDc(Notice t) {
		DetachedCriteria dc = super.getDc(t);

		if (t.getPublishTime() != null) {
			dc.add(Restrictions.le("publishTime", t.getPublishTime()));
		}
		if (t.getPublicityPeriod() != null) {
			dc.add(Restrictions.ge("publicityPeriod", t.getPublicityPeriod()));
		}
		if (!StringUtils.isBlank(t.getTitle())) {
			dc.add(Restrictions.like("title", t.getTitle(), MatchMode.ANYWHERE));
		}
		if (!StringUtils.isBlank(t.getContent())) {
			dc.add(Restrictions.like("content", t.getContent(), MatchMode.ANYWHERE));
		}
		if (t.getInPeriod() != null) {
			dc.add(Restrictions.isNotNull("publicityPeriod"));
			dc.add(Restrictions.sqlRestriction("adddate({alias}.publishTime, {alias}.publicityPeriod) " + (t.getInPeriod() ? ">=" : "<") + " now()"));
		}

		return dc;
	}

}
